/*
Copyright (c) 2010, Jonathan Baldwin
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this
  list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this
  list of conditions and the following disclaimer in the documentation and/or
  other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef RECAP_FRAMEQUEUE_H
#define RECAP_FRAMEQUEUE_H
#include <QtCore>

namespace recap
{
	class FrameQueue
	{
	public:
	  class Reader
	  {
	  public:
	    Reader(const Reader &other);
	    Reader& operator=  (const Reader &other);
	    
	    operator const char* () const;
	    const char&  operator[] (unsigned int index) const;
	    
	    Reader& operator++ ();
	    void    operator++ (int) const;
	  
	    unsigned int frameSize() const;
	    bool    hasQueuedData () const;
	  protected:
	    Reader (FrameQueue &buffer);
	  private:
	    FrameQueue  *m_buffer;
	    friend class FrameQueue;
	  };
	  class Writer
	  {
	  public:
	    Writer (const Writer &other);
	    Writer& operator=  (const Writer &other);
	    
	    operator char* () const;
	    char&  operator[] (unsigned int index) const;
	    
	    Writer& operator++ ();
	    void    operator++ (int) const;
	    
	    unsigned int frameSize() const;
	  protected:
	    Writer (FrameQueue &buffer);
	  private:
	    FrameQueue *m_buffer;  
	    friend class FrameQueue;
	  };
	  
	  FrameQueue(unsigned int frameSize=0x200);
	  ~FrameQueue();
	  Reader reader();
	  Writer writer();
	  
	  unsigned int readFrameNumber() const;
	  unsigned int writeFrameNumber() const;
	  unsigned int frameSize() const;
	  void setFrameSize(unsigned int frameSize);
	  bool hasQueuedData() const;
	  void clear();
	private:
	  
	  void increment_read_pointer();
	  void increment_write_pointer();
	  
	  char *get_read_pointer();
	  char *get_write_pointer();
	  char *get_pointer(unsigned int queued_frame);
	  
	  unsigned int m_read_frame_number, m_write_frame_number;
	  unsigned int m_frame_size;
	  QMap<unsigned int, char *> m_data_array;
	  mutable QReadWriteLock m_lock;
	  
	  friend class Reader;
	  friend class Writer;
	};
	class FrameQueueDevice : public QIODevice
	{
	  Q_OBJECT
	public:
	  FrameQueueDevice(recap::FrameQueue& fq, QObject* parent = 0);
	protected:
	  virtual qint64 readData(char* data, qint64 maxlen);
	  virtual qint64 writeData(const char* data, qint64 len);
	private:
	  FrameQueue::Reader m_reader;
	  FrameQueue::Writer m_writer;
	  qint64             m_wOffset;
	};
}

#endif
